package com.xinfu.newproject.ui.resident_certifition;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alley.van.VanGogh;
import com.alley.van.helper.VanCropType;
import com.alley.van.helper.VanMediaFilter;
import com.alley.van.helper.VanMediaType;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.ChooseCommunityActivity;
import com.xinfu.newproject.activity.ResidentCertificationActivity;
import com.xinfu.newproject.adapter.matter_repair_adapter.RepairImageAdapter;
import com.xinfu.newproject.base.BaseFragment;
import com.xinfu.newproject.bean.certifition.AddRoomBean;
import com.xinfu.newproject.bean.certifition.QeuryRoomBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.up_image.UpImageBean;
import com.xinfu.newproject.contants.Contants;
import com.xinfu.newproject.presenter.AddRoomPresenter;
import com.xinfu.newproject.presenter.QeuryRoomPresenter;
import com.xinfu.newproject.presenter.UpImagePresenter;
import com.xinfu.newproject.test.GifSizeFilter;
import com.xinfu.newproject.test.GlideImageLoader;
import com.xinfu.newproject.test.VanToast;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.GetImagePathUtil;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.util.WHelperUtil;
import com.xinfu.newproject.view.IView;
import com.xinfu.newproject.widget.room_picker.AddressPickerView;
import com.xinfu.newproject.widget.room_picker.YwpAddressBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static android.app.Activity.RESULT_OK;
import static com.xinfu.newproject.activity.ResidentCertificationActivity.FRAGMENT_ZERO;
import static com.xinfu.newproject.contants.Constant.LOGIN;
import static com.xinfu.newproject.contants.Contants.REQUEST_CODE_AVATAR;
import static com.xinfu.newproject.contants.Contants.REQUEST_CODE_IMAGE;

public class MyCertifitionFragment extends BaseFragment implements TextWatcher, IView.IQueryRoomView, IView.IAddRoomView, RepairImageAdapter.RemoveOnClickListener, IView.IUpImageView {

    @BindView(R.id.head_text)
    TextView titleTv;

    @BindView(R.id.fragment_my_certifition_id)
    TextView idTv;

    @BindView(R.id.layout_progress_tv2)
    TextView oneTv2;

    @BindView(R.id.layout_progress_tv3)
    TextView oneTv3;

    @BindView(R.id.layout_progress_tv4)
    TextView oneTv4;

    @BindView(R.id.layout_progress_two_tv1)
    TextView twoTv1;

    @BindView(R.id.layout_progress_two_tv2)
    TextView twoTv2;

    @BindView(R.id.layout_progress_two_tv3)
    TextView twoTv3;

    @BindView(R.id.layout_progress_two_tv4)
    TextView twoTv4;

    @BindView(R.id.layout_progress_v1)
    View v1;

    @BindView(R.id.layout_progress_v2)
    View v2;

    @BindView(R.id.layout_progress_two_v2)
    View twoV2;

    @BindView(R.id.layout_progress_v3)
    View v3;

    @BindView(R.id.layout_progress_two_v3)
    View twoV3;

    @BindView(R.id.layout_progress_v4)
    View v4;

    @BindView(R.id.layout_progress_rl4)
    RelativeLayout progressRl4;

    @BindView(R.id.fragment_my_certifition_name_et)
    EditText nameEt;

    @BindView(R.id.fragment_my_certifition_phone_et)
    EditText phoneEt;

    @BindView(R.id.fragment_my_certifition_phone_iv)
    ImageView phoneCloseIv;

    @BindView(R.id.fragment_my_certifition_name_iv)
    ImageView nameCloseIv;

    @BindView(R.id.fragment_my_certifition_address_tv)
    TextView addressTv;

    @BindView(R.id.fragment_my_certifition_room_tv)
    TextView roomTv;

    @BindView(R.id.layout_next_step_tv)
    TextView nextTv;

    @BindView(R.id.fragment_my_certifition_LL)
    LinearLayout mLL;

    @BindView(R.id.include_auth_owner_view)
    LinearLayout ownerLL;

    @BindView(R.id.include_auth_tenant_view)
    LinearLayout tenantLL;

    @BindView(R.id.include_auth_owner_rv)
    RecyclerView ownerRv;

    @BindView(R.id.include_auth_tenant_rv)
    RecyclerView tenantRv;

    @BindView(R.id.include_auth_owner_et)
    EditText areaEt;

    @BindView(R.id.layout_auth_tenant_name_et)
    EditText tenantNameEt;

    @BindView(R.id.layout_auth_tenant_phone_et)
    EditText tenantPhoneEt;

    int authType, idType;
    private static final int REQUEST_CODE_CHOOSE = 23;
    private static final int REQUEST_CODE_CAMERA = 32;
    private MyCertifitionFragment fragment;

    public static MyCertifitionFragment newInstance() {
        MyCertifitionFragment myCertifitionFragment = new MyCertifitionFragment();
        Bundle bundle = new Bundle();
//        bundle.putInt(Integer.class.getName(), resId);
        myCertifitionFragment.setArguments(bundle);
        return myCertifitionFragment;
    }

    private PopupWindow popMenu;

    private QeuryRoomPresenter qeuryRoomPresenter = new QeuryRoomPresenter(this);
    private AddRoomPresenter addRoomPresenter = new AddRoomPresenter(this);
    private UpImagePresenter upImagePresenter = new UpImagePresenter(this);

    private BackHandledInterface backHandledInterface;
    private RepairImageAdapter repairImageAdapter;

    private int isImage = 0;
    private ArrayList<String> imageList = new ArrayList<>();

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_my_certifition;
    }

    @Override
    protected void setUpView() {
        titleTv.setText("我的认证");
        nextTv.setText("提交");
        initHeadProgeress();
        fragment = this;
        ownerRv.setLayoutManager(new GridLayoutManager(getMContext(), 4));
        tenantRv.setLayoutManager(new GridLayoutManager(getMContext(), 4));
        repairImageAdapter = new RepairImageAdapter(this);
        ownerRv.setAdapter(repairImageAdapter);
        tenantRv.setAdapter(repairImageAdapter);
        imageList.add(0, "0");
        repairImageAdapter.setNewData(imageList);
        repairImageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("CheckResult")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (imageList.get(position).equals("0")) {
                    isCode = REQUEST_CODE_AVATAR;
                    onClickCamera();
                } else {
                    isCode = REQUEST_CODE_IMAGE;
                    imagePosition = position;
                    onClickCamera();
                }
            }
        });
    }

    int isCode;

    @Override
    public void permissionGrant(boolean isGranted, int requestCode) {
        super.permissionGrant(isGranted, requestCode);
        if (!isGranted) {
            return;
        }
        switch (requestCode) {

            case 0x0002://从相册选择
                int maxCount;
                if (isCode == REQUEST_CODE_AVATAR) {
                    maxCount = 5 - imageList.size();
                } else {
                    maxCount = 4;
                }
                VanGogh.from(fragment)
                        .choose(VanMediaType.ofAll())
                        .countable(true)//若开启裁剪，则无效
                        .maxCount(maxCount)
                        .rowCount(3)
                        .cameraVisible(true, getActivity().getPackageName())//设置在第一个参数为FALSE时，第二个参数无效
                        .withResultSize(1024, 1024)
                        .cropEnable(false, VanCropType.CROP_TYPE_RECTANGLE)//第一个参数为TRUE时，则可选中数量被设为1，此时maxSelectable(9)无效；第一个参数为FALSE时，第二个参数无效
                        .theme(R.style.VanTheme_Dracula)
                        .addFilter(new GifSizeFilter(320, 320, 5 * VanMediaFilter.K * VanMediaFilter.K))
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                        .thumbnailScale(0.85f)
                        .toast(new VanToast())
                        .imageLoader(new GlideImageLoader())
                        .forResult(REQUEST_CODE_CHOOSE);
                break;

            default:
                break;
        }
    }
    private void initHeadProgeress() {
        v4.setVisibility(View.GONE);
        twoV3.setVisibility(View.GONE);
        oneTv4.setVisibility(View.GONE);
        twoTv4.setVisibility(View.GONE);
        v2.setBackgroundResource(R.color.yellow_ff);
        v1.setBackgroundResource(R.color.yellow_ff);
        progressRl4.setVisibility(View.GONE);
        twoTv1.setText("提交认证");
        twoTv2.setText("审核中");
        twoTv3.setText("认证成功");

    }

    @Override
    protected void setUpData() {
        nameEt.addTextChangedListener(this);
        phoneEt.addTextChangedListener(this);
        idTv.addTextChangedListener(this);
    }

    @OnClick({R.id.fragment_my_certifition_id, R.id.fragment_my_certifition_address_tv, R.id.layout_next_step_tv, R.id.fragment_my_certifition_room_tv, R.id.head_back, R.id.include_auth_owner_authType, R.id.layout_auth_tenant_authType
            , R.id.fragment_my_certifition_name_iv, R.id.fragment_my_certifition_phone_iv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.head_back:

                backHandledInterface = (BackHandledInterface) getMContext();
                backHandledInterface.setSelectedFragment(FRAGMENT_ZERO);
                break;
            case R.id.fragment_my_certifition_id:
                initPopMenu();
                popMenu.showAtLocation(idTv, Gravity.BOTTOM, 0, 0);
                backgroundAlpha(0.5f);
                break;
            case R.id.fragment_my_certifition_address_tv:
                startActivityForResult(new Intent(getMContext(), ChooseCommunityActivity.class).putExtra("type", Contants.CHOOSECOMMUNITYCERTIFITION), Contants.CHOOSECOMMUNITYCERTIFITION);
                break;
            case R.id.fragment_my_certifition_room_tv:
                int isOk = isAddRoom(0);
                if (isOk == 1) {
                    qeuryRoomPresenter.getRoom();
                } else if (isOk == 2) {
                    BToast.showText(getMContext(), "请填写正确的手机号");
                } else {
                    BToast.showText(getMContext(), "请填写完整信息");
                }

                break;
            case R.id.layout_next_step_tv:
                int isOk2 = isAddRoom(1);
                if (isOk2 == 1 && idType != 0) {
                    if (imageList.size() > 1) {
                        ((ResidentCertificationActivity) getMContext()).showLoding();
                        upImagePresenter.upImage();
                    } else {
                        if (idType == 1 && authType == 0) {
                            if (TextUtils.isEmpty(areaEt.getText())) {
                                BToast.showText(getMContext(), "请填写房屋面积");
                            } else {
                                addRoomPresenter.addRoom();
                            }
                        } else if (idType == 2 && authType == 0) {
                            if (TextUtils.isEmpty(tenantNameEt.getText()) || TextUtils.isEmpty(tenantPhoneEt.getText())) {
                                BToast.showText(getMContext(), "请填写完整业主信息");
                            } else {
                                addRoomPresenter.addRoom();
                            }
                        } else if (idType == 3 && authType == 0) {
                            if (TextUtils.isEmpty(tenantNameEt.getText()) || TextUtils.isEmpty(tenantPhoneEt.getText())) {
                                BToast.showText(getMContext(), "请填写完整业主信息");
                            } else {
                                addRoomPresenter.addRoom();
                            }
                        }

                    }
                } else if (isOk2 == 2) {
                    BToast.showText(getMContext(), "请填写正确的手机号");
                } else {
                    BToast.showText(getMContext(), "请填写完整信息");
                }
                break;
            case R.id.include_auth_owner_authType:
                if (authType == 0) {
                    if (idType == 1) {
                        getContentView().findViewById(R.id.include_auth_owner_ll).setVisibility(View.VISIBLE);
                        getContentView().findViewById(R.id.include_auth_owner_rl).setVisibility(View.GONE);
                    }
                    authType = 1;
                } else if (authType == 1) {
                    if (idType == 1) {
                        getContentView().findViewById(R.id.include_auth_owner_ll).setVisibility(View.GONE);
                        getContentView().findViewById(R.id.include_auth_owner_rl).setVisibility(View.VISIBLE);
                    }
                    authType = 0;
                }
                imageList.clear();
                imageList.add(0, "0");
                repairImageAdapter.setNewData(imageList);
                break;
            case R.id.layout_auth_tenant_authType:
                if (authType == 0) {
                    if (idType == 2) {
                        getContentView().findViewById(R.id.include_auth_tenant_ll).setVisibility(View.GONE);
                        getContentView().findViewById(R.id.include_auth_tenant_ll2).setVisibility(View.VISIBLE);
                    } else if (idType == 3) {
                        getContentView().findViewById(R.id.include_auth_tenant_ll).setVisibility(View.VISIBLE);
                        getContentView().findViewById(R.id.include_auth_tenant_ll2).setVisibility(View.GONE);
                    }
                    authType = 1;
                } else if (authType == 1) {
                    if (idType == 2) {
                        getContentView().findViewById(R.id.include_auth_tenant_ll).setVisibility(View.VISIBLE);
                        getContentView().findViewById(R.id.include_auth_tenant_ll2).setVisibility(View.GONE);
                    } else if (idType == 3) {
                        getContentView().findViewById(R.id.include_auth_tenant_ll).setVisibility(View.GONE);
                        getContentView().findViewById(R.id.include_auth_tenant_ll2).setVisibility(View.VISIBLE);
                    }
                    authType = 0;
                }
                imageList.clear();
                imageList.add(0, "0");
                repairImageAdapter.setNewData(imageList);
                break;
            case R.id.fragment_my_certifition_name_iv:
                nameEt.getText().clear();
                break;
            case R.id.fragment_my_certifition_phone_iv:
                phoneEt.getText().clear();
                break;
        }
    }


    private void initPopMenu() {

        View contentView = View.inflate(getMContext(), R.layout.pop_my_certifition_id,
                null);
        popMenu = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        popMenu.setOutsideTouchable(true);
        popMenu.setBackgroundDrawable(new BitmapDrawable());
        popMenu.setFocusable(true);
        popMenu.setAnimationStyle(R.style.popwin_anim_style);
        popMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            public void onDismiss() {
//                productTv.setTextColor(Color.parseColor("#5a5959"));
//                sortTv.setTextColor(Color.parseColor("#5a5959"));
//                activityTv.setTextColor(Color.parseColor("#5a5959"));
                backgroundAlpha(1);
            }
        });

        contentView.findViewById(R.id.pop_my_certifition_close_iv)
                .setOnClickListener(new View.OnClickListener() {
                    public void onClick(View arg0) {
                        popMenu.dismiss();
                        backgroundAlpha(1);
                    }
                });
        contentView.findViewById(R.id.item_pop_text1)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        idTv.setText("业主");
                        idTv.setTextColor(getMContext().getResources().getColor(R.color.location_color));
                        authType = 0;
                        idType = 1;
                        getContentView().findViewById(R.id.include_auth_owner_ll).setVisibility(View.GONE);
                        getContentView().findViewById(R.id.include_auth_owner_rl).setVisibility(View.VISIBLE);
                        imageList.clear();
                        imageList.add(0, "0");
                        repairImageAdapter.setNewData(imageList);
                        popMenu.dismiss();
                        backgroundAlpha(1);
                    }
                });
        contentView.findViewById(R.id.item_pop_text2)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        idTv.setText("租户");
                        idTv.setTextColor(getMContext().getResources().getColor(R.color.location_color));
                        authType = 0;
                        idType = 2;
                        getContentView().findViewById(R.id.include_auth_tenant_ll).setVisibility(View.GONE);
                        getContentView().findViewById(R.id.include_auth_tenant_ll2).setVisibility(View.VISIBLE);
                        imageList.clear();
                        imageList.add(0, "0");
                        repairImageAdapter.setNewData(imageList);
                        popMenu.dismiss();
                        backgroundAlpha(1);
                    }
                });
        contentView.findViewById(R.id.item_pop_text3)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        idTv.setText("同住人");
                        idTv.setTextColor(getMContext().getResources().getColor(R.color.location_color));
                        authType = 0;
                        idType = 3;
                        getContentView().findViewById(R.id.include_auth_tenant_ll).setVisibility(View.GONE);
                        getContentView().findViewById(R.id.include_auth_tenant_ll2).setVisibility(View.VISIBLE);
                        imageList.clear();
                        imageList.add(0, "0");
                        repairImageAdapter.setNewData(imageList);
                        popMenu.dismiss();
                        backgroundAlpha(1);
                    }
                });

    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getActivity().getWindow().setAttributes(lp);
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }


    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (nameEt.getText().length() > 0) {
            nameCloseIv.setVisibility(View.VISIBLE);
        } else {
            nameCloseIv.setVisibility(View.GONE);
        }
        if (phoneEt.getText().length() > 0) {
            phoneCloseIv.setVisibility(View.VISIBLE);
        } else {
            phoneCloseIv.setVisibility(View.GONE);
        }

        if (idTv.getText().toString().equals("业主")) {
            ownerLL.setVisibility(View.VISIBLE);
            tenantLL.setVisibility(View.GONE);
        } else if (idTv.getText().toString().equals("租户")) {
            ownerLL.setVisibility(View.GONE);
            tenantLL.setVisibility(View.VISIBLE);
            getContentView().findViewById(R.id.layout_auth_tenant_type_rl).setVisibility(View.VISIBLE);
        } else if (idTv.getText().toString().equals("同住人")) {
            ownerLL.setVisibility(View.GONE);
            tenantLL.setVisibility(View.VISIBLE);
            getContentView().findViewById(R.id.layout_auth_tenant_type_rl).setVisibility(View.GONE);
        }
    }

    /**
     * 选择房产
     *
     * @param options1Items
     * @param options2Items
     */
    String roomId;

    private void showPopWindow(ArrayList<String> options1Items, ArrayList<ArrayList<String>> options2Items, ArrayList<String> options1IDItems, ArrayList<ArrayList<String>> options2IdItems) {
        List<YwpAddressBean.AddressItemBean> list = new ArrayList<>();
        List<YwpAddressBean.AddressItemBean> list1 = new ArrayList<>();
        for (int i = 0; i < options1IDItems.size(); i++) {
            YwpAddressBean.AddressItemBean addressItemBean = new YwpAddressBean.AddressItemBean();
            addressItemBean.setI(options1IDItems.get(i));
            addressItemBean.setN(options1Items.get(i));
            addressItemBean.setP(options1IDItems.get(i));
            list.add(addressItemBean);
        }
        for (int i = 0; i < options2Items.size(); i++) {
            for (int j = 0; j < options2Items.get(i).size(); j++) {
                YwpAddressBean.AddressItemBean addressItemBean = new YwpAddressBean.AddressItemBean();
                addressItemBean.setN(options2Items.get(i).get(j));
                addressItemBean.setI(options2IdItems.get(i).get(j));
                addressItemBean.setP(options1IDItems.get(i));
                list1.add(addressItemBean);
            }
        }
        YwpAddressBean ywpAddressBean = new YwpAddressBean();
        ywpAddressBean.setProvince(list);
        ywpAddressBean.setCity(list1);

        final PopupWindow popupWindow = new PopupWindow(getMContext());
        View rootView = LayoutInflater.from(getMContext()).inflate(R.layout.pop_address_picker, null, false);
        ImageView imageView = rootView.findViewById(R.id.pop_address_picker_close);

        AddressPickerView addressView = rootView.findViewById(R.id.pop_address_picker_apvAddress);
        addressView.initData(ywpAddressBean);
        addressView.setOnAddressPickerSure(new AddressPickerView.OnAddressPickerSureListener() {
            @Override
            public void onSureClick(String room_id, String provinceCode, String cityCode) {

                //返回的分别是三个级别的选中位置
                String buildName = provinceCode;
                String roomName = cityCode;

                if (!TextUtils.isEmpty(buildName) && !TextUtils.isEmpty(roomName)) {
                    //赋值
                    roomTv.setText(provinceCode + " " + cityCode);
                    /**
                     * 得到Id
                     */
                    roomId = room_id;

                } else {
                    roomTv.setText("");
                    roomId = "";
//                    showIncompleteAlertDialog(getMContext(), "请您选择房产！");
                }
                Log.i("testRoom", roomTv.getText().toString() + "roomId:  " + room_id);
                popupWindow.dismiss();
            }
        });
        popupWindow.setContentView(rootView);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.showAtLocation(roomTv, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });

    }


    /**
     * 判断是否可以请求添加房间号
     *
     * @return
     */
    String token, userId, userName;

    private Integer isAddRoom(int type) {
        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(getMContext(), LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            return 0;
        }
        if (projectId == null) {
            return 0;
        }

        if (type == 1) {
            if (roomId == null) {
                return 0;
            }

            if (TextUtils.isEmpty(nameEt.getText())) {
                return 0;
            }

            if (TextUtils.isEmpty(phoneEt.getText())) {
                return 0;
            } else {
                if (!WHelperUtil.isMobileRight(getMContext(), phoneEt.getText().toString())) {
                    return 2;
                }
            }
        }

        if (responseIntent == null) {

            return 0;
        }
        return 1;
    }

    @Override
    public String header() {
        return token;
    }

    @Override
    public List<String> imageList() {
        return imageList;
    }

    @Override
    public Context context() {
        return getMContext();
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String userName() {
        return userName;
    }

    @Override
    public String roomId() {
        return roomId;
    }

    @Override
    public String applyName() {
        return nameEt.getText().toString().trim();
    }

    @Override
    public String applyTel() {
        return phoneEt.getText().toString().trim();
    }

    @Override
    public String address() {
        return roomTv.getText().toString().trim();
    }

    @Override
    public Integer idType() {
        int idType = 1;
        switch (idTv.getText().toString()) {
            case "业主":
                idType = 1;
                break;
            case "租户":
                idType = 2;
                break;
            case "同住人":
                idType = 3;
                break;
        }
        return idType;
    }

    @Override
    public String imgUrl() {
        return attachFile;
    }

    @Override
    public String applyCusName() {
        return tenantNameEt.getText().toString().trim();
    }

    @Override
    public String applyCusTel() {
        return tenantPhoneEt.getText().toString().trim();
    }

    @Override
    public String area() {
        return areaEt.getText().toString().trim();
    }

    @Override
    public String projectId() {
        return projectId;
    }

    @Override
    public void showToast(String content) {
        BToast.showText(getMContext(), content);
    }

    @Override
    public void showProgress() {
        ((ResidentCertificationActivity) getMContext()).showLoding();
    }

    @Override
    public void hideProgress() {
        ((ResidentCertificationActivity) getMContext()).hideLoding();
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    String attachFile;

    @Override
    public void getData(UpImageBean upImageBean) {
        attachFile = upImageBean.getResult();
        Log.i("testImage", isImage + "");
        if (attachFile != null) {
            if (isImage == 1) {
                addRoomPresenter.addRoom();
            }
        }
    }


    @Override
    public void getData(AddRoomBean addRoomBean) {
        if (addRoomBean.getMessage() != null) {
            BToast.showText(getMContext(), addRoomBean.getMessage());
        }
        backHandledInterface = (BackHandledInterface) getMContext();
        backHandledInterface.setSelectedFragment(FRAGMENT_ZERO);
    }

    List<QeuryRoomBean.ResultBean> resultBean;

    @Override
    public void getData(QeuryRoomBean qeuryRoomBean) {
        resultBean = qeuryRoomBean.getResult();
        ArrayList<String> room1 = new ArrayList<>();
        ArrayList<String> roomIdList = new ArrayList<>();
        ArrayList<ArrayList<String>> room2 = new ArrayList<>();
        ArrayList<ArrayList<String>> room2IdList = new ArrayList<>();
        for (int i = 0; i < qeuryRoomBean.getResult().size(); i++) {
            room1.add(qeuryRoomBean.getResult().get(i).getName());
            roomIdList.add(qeuryRoomBean.getResult().get(i).getId());
            ArrayList<String> list = new ArrayList<>();
            ArrayList<String> list1 = new ArrayList<>();
            for (int j = 0; j < qeuryRoomBean.getResult().get(i).getRoomList().size(); j++) {
                list.add(qeuryRoomBean.getResult().get(i).getRoomList().get(j).getName());
                list1.add(qeuryRoomBean.getResult().get(i).getRoomList().get(j).getId());
            }
            room2.add(list);
            room2IdList.add(list1);
        }
        if (room1.size() != 0 && room2.size() != 0) {
            showPopWindow(room1, room2, roomIdList, room2IdList);
        } else {
            BToast.showText(getMContext(), "数据为空");
        }
    }


    /**
     * 获取选择的小区
     */
    private String projectName, projectId, tenantId, address;
    Integer imagePosition;


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            final ArrayList<String> pathList = new ArrayList<>();
            switch (requestCode) {
                case Contants.CHOOSECOMMUNITYCERTIFITION:
                    if (data.getStringExtra("name") != null) {
                        projectName = data.getExtras().getString("name");
                        projectId = data.getExtras().getString("communityId");
                        tenantId = data.getExtras().getString("tenantId");
                        address = data.getExtras().getString("address");
                        addressTv.setText(data.getStringExtra("name"));

                    }
                    break;
                case REQUEST_CODE_CHOOSE:
                    if (VanGogh.obtainResult(data) != null) {
                        if (VanGogh.obtainResult(data).size() > 0) {
                            for (int i = 0; i < VanGogh.obtainResult(data).size(); i++) {
                                pathList.add(GetImagePathUtil.getRealFilePath(getMContext(), VanGogh.obtainResult(data).get(i)));
                            }
                        }
                    }
                    if (isCode == REQUEST_CODE_AVATAR) {
                        if ((imageList.size() - 1) < pathList.size()) {
                            if (imageList.size() == 1) {
                                imageList.clear();
                            } else {
                                imageList.remove(imageList.size() - 1);
                            }
                            for (int i = 0; i < pathList.size(); i++) {
                                imageList.add(i, pathList.get(i));
                            }
                        } else if ((5 - imageList.size()) >= pathList.size()) {
                            if (imageList.size() == 1) {
                                imageList.clear();
                            } else {
                                imageList.remove(imageList.size() - 1);
                            }
                            for (int i = 0; i < pathList.size(); i++) {
                                imageList.add(pathList.get(i));
                            }
                        }
                        isImage = 1;
                    } else {
                        if (pathList != null) {
                            if (pathList.size() > 0) {
                                if (pathList.size() == 1 && !pathList.get(0).equals("")) {
                                    if (imagePosition != null) {
                                        imageList.set(imagePosition, pathList.get(0));
                                    }
                                } else {
                                    if (!pathList.get(0).equals("")) {
                                        imageList.clear();
                                        for (int i = 0; i < pathList.size(); i++) {
                                            imageList.add(i, pathList.get(i));
                                        }
                                    }
                                }
                            }
                        }
                        imagePosition = null;
                        isImage = 1;
                    }
                    break;
            }
            for (int i = 0; i < imageList.size(); i++) {
                if (imageList.get(i).equals("0")) {
                    imageList.remove(i);
                }
            }
            if (imageList.size() < 4) {
                imageList.add("0");
            }
            repairImageAdapter.setNewData(imageList);
        }
    }


    @Override
    public void removeImage(int position) {
        imageList.remove(position);
        for (int i = 0; i < imageList.size(); i++) {
            if (imageList.get(i).equals("0")) {
                imageList.remove(i);
            }
        }
        if (imageList.size() < 4) {
            imageList.add("0");
        }
        repairImageAdapter.setNewData(imageList);

    }
}
